import pandas as pd

def divide_week(file_in, flag):
    df = pd.DataFrame(pd.read_csv(file_in))
    df['time'] = pd.to_datetime(df['time'])   # 将时间转换为datetime类型
    df['dayofweek'] = df['time'].dt.dayofweek   # 输出这一天是周中的第几天, Monday=0, Sunday=6
    temp = df['dayofweek'].unique()
    for index, element in enumerate(temp.flat):
        div = df.loc[df['dayofweek'] == element]
        week = str(element + 1)
        if flag == 0:  # 判断是否为第一次写文件，决定是否写入header
            div.to_csv('..\\data\\divide_week\\week_' + week + '.csv', mode="a", index=False)
            print(div)
        else:
            div.to_csv('..\\data\\divide_week\\week_' + week + '.csv', mode="a", index=False, header=0)
            print(div)


if __name__ == '__main__':
    for month in range(6, 10):
        flag = 1
        if month == 6:
            flag = 0
        filein = '..\\data\\clean_file\\clean_file180' + str(month) + '.csv'
        print(filein)
        divide_week(filein, flag)
